# import sys
# # content roots
# sys.path.append("/Users/tonywu/Public/Test/tornadoTest/")
# # source roots
# sys.path.append("/Users/tonywu/Public/Test/tornadoTest/app01/templates")
from tornado import httpserver
from tornado.ioloop import IOLoop
from urls import settings
from tornado.web import Application
from tornado.options import define, options, parse_command_line


class Sever(Application):
    def __init__(self, port=8000, host="127.0.0.1", multi=False):
        self.multi = multi
        if multi:
            self.server = httpserver.HTTPServer(self)
            self.server.listen(port, host)
        else:
            self.listen(port, host)
        Application.__init__(self, **settings)

    def start(self, num=0):
        if self.multi:
            # 0 表示自动，进程数 = cpu核数 + 1
            self.server.start(num_processes=num)
        IOLoop.instance().start()


if __name__ == '__main__':
    define(name="host", default="127.0.0.1", type=str, help="设置监听地址，默认127.0.0.1")
    define(name="port", default=8889, type=int, help="设置端口号，默认8889")
    define(name="fork", default=1, type=int, help="设置线程数，默认1")
    define(name="debug", default=True, type=bool, help="设置调试模式，默认True")
    parse_command_line()

    settings['debug'] = options.debug
    if options.fork != 1:
        app = Sever(options.port, options.host, multi=True)
        app.start(options.fork)
    else:
        app = Sever(options.port, options.host)
        app.start()